package nfwx.analytics;

/**
 * 采集参数，基类：采集参数包含两个状态（是否发生修改和是否存在值), 任何子参数的修改和值设置都会影响父采集参数的变化。
 */

public class  GrabValue {
    ///表示本级节点下值发生修改的子节点数
    protected int _changedCount;
    ///表示本级节点下有值的子节点数
    protected int _hasValueCount;
    ///在序列化时是否必须存在值，如果值不存在则抛出异常
    protected boolean _mustHasValue;
    ///父节点
    protected GrabValue _parent;

    GrabValue(boolean mustHasValue)
    {
        _changedCount=0;
        _hasValueCount=0;
        _mustHasValue = mustHasValue;
    }

    /**
     * 增加修改计数
     */
    protected void incChanged()
    {
        if (this instanceof GrabObject || this instanceof GrabArray || _changedCount == 0) {
            _changedCount++;
            if (_parent != null && _changedCount == 1) {
                _parent.incChanged();
            }
        }
    }

    /**
     * 递减修改计数
     */
    protected void decChanged()
    {
        if (_changedCount == 0) return;
        _changedCount--;
        if (_changedCount == 0 && _parent != null) {
            _parent.decChanged();
        }
    }

    /**
     * 增加有值计数
     */
    protected void incHasValue()
    {
        if (this instanceof GrabObject || this instanceof GrabArray || _hasValueCount == 0) {
            _hasValueCount++;
            if (_parent != null && _hasValueCount == 1) {
                _parent.incHasValue();
            }
        }
    }

    /**
     * 递减有值计数
     */
    protected void decHasValue()
    {
        if (_hasValueCount == 0) return;
        _hasValueCount--;
        if (_hasValueCount == 0 && _parent != null) {
            _parent.decHasValue();
        }
    }

    /**
     * 重置修改
     */
    public void resetChanged()
    {
        if (_changedCount == 0) return;
        if (!(this instanceof GrabObject || this instanceof GrabArray)) {
            decChanged();
        }
    }

    /**
     * 重置有值
     */
    public void resetValue()
    {
        if (_hasValueCount == 0) return;
        if (!(this instanceof GrabObject || this instanceof GrabArray)) {
            decHasValue();
        }
    }

    public boolean getChanged()
    {
        return _changedCount != 0;
    }

    public boolean getHasValue()
    {
        return _hasValueCount != 0;
    }

    public boolean getMustHasValue()
    {
        return _mustHasValue;
    }

}

